﻿package  
{
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Loader;
	import flash.net.URLRequest;
	
	
	public class CreatePlayerUI extends MovieClip 
	{
		public var Vitality:int = 0;
		public var Strength:int = 0;
		public var Toughness:int = 0;
		public var Dexterity:int = 0;
		
		public var earth:int = 0;
		public var water:int = 0;
		public var fire:int = 0;
		public var wind:int = 0;
		
		public var attrTotal:int = 0;
		public var MAXTOTAL:int = 10;
		
		public var baseTotal:int = 0;
		public var MAXBASE:int = 20;
		
		public var headID:int = 0;
		public var id:int = 0;
		public var color:int = 0;
		public var eye:int = 0;
		public var mouth:int = 0;
		
		public var loader:Loader = null;
		
		public function CreatePlayerUI()
		{
			// constructor code
			
			eyeRightButton.addEventListener( MouseEvent.CLICK , onEyeRight );
			eyeLeftButton.addEventListener( MouseEvent.CLICK , onEyeLeft );
			mouthRightButton.addEventListener( MouseEvent.CLICK , onMouthRight );
			mouthLeftButton.addEventListener( MouseEvent.CLICK , onMouthLeft );
			
			earthRightButton.addEventListener( MouseEvent.CLICK , onEarthRight );
			earthLeftButton.addEventListener( MouseEvent.CLICK , onEarthLeft );
			
			waterRightButton.addEventListener( MouseEvent.CLICK , onWaterRight );
			waterLeftButton.addEventListener( MouseEvent.CLICK , onWaterLeft );
			
			fireRightButton.addEventListener( MouseEvent.CLICK , onFireRight );
			fireLeftButton.addEventListener( MouseEvent.CLICK , onFireLeft );
			
			windRightButton.addEventListener( MouseEvent.CLICK , onWindRight );
			windLeftButton.addEventListener( MouseEvent.CLICK , onWindLeft );
			
			
			vRightButton.addEventListener( MouseEvent.CLICK , onVRight );
			vLeftButton.addEventListener( MouseEvent.CLICK , onVLeft );
			
			sRightButton.addEventListener( MouseEvent.CLICK , onSRight );
			sLeftButton.addEventListener( MouseEvent.CLICK , onSLeft );
			
			tRightButton.addEventListener( MouseEvent.CLICK , onTRight );
			tLeftButton.addEventListener( MouseEvent.CLICK , onTLeft );
			
			dRightButton.addEventListener( MouseEvent.CLICK , onDRight );
			dLeftButton.addEventListener( MouseEvent.CLICK , onDLeft );
			
			
			checkAttr();
			checkBase();
			
			loader = new Loader();
			loader.x = -259;
			loader.y = -105;
			addChild( loader );
		}
		
		
		public function onEyeRight( event:MouseEvent ):void
		{
			eye++;
			if ( eye > 4 )
			{
				eye = 0;
			}
			
			updateTexture();
		}
		public function onEyeLeft( event:MouseEvent ):void
		{
			eye--;
			if ( eye < 0 )
			{
				eye = 4;
			}
			
			updateTexture();
		}
		public function onMouthRight( event:MouseEvent ):void
		{
			mouth++;
			if ( mouth > 4 )
			{
				mouth = 0;
			}
			
			updateTexture();
		}
		public function onMouthLeft( event:MouseEvent ):void
		{
			mouth--;
			if ( mouth < 0 )
			{
				mouth = 4;
			}
			
			updateTexture();
		}
		
		public function updateTexture():void
		{
			headID = 7367 + 100 * id + 25 * color + 5 * eye + mouth;
			
			loader.load( new URLRequest( '../role/' + headID.toString() + ".png" ) );
		}
		
		public function setData( i:int , c:int ):void
		{
			clear();
			
			id = i;
			color = c;
			eye = 0;
			mouth = 0;
			
			if ( color > 3 )
			{
				color = 0;
			}
			
			updateTexture();
			
		}
		
		
		public function clear():void
		{
			Vitality = 0;
			Strength = 0;
			Toughness = 0;
			Dexterity = 0;
			
			earth = 0;
			water = 0;
			fire = 0;
			wind = 0;
			
			attrTotal = 0;
			MAXTOTAL = 10;
			
			baseTotal = 0;
			MAXBASE = 20;
			
			nameText.text = "";
			
			checkAttr();
			checkBase();
		}
		
		public function setBase():void
		{
			vText.text = Vitality.toString();
			sText.text = Strength.toString();
			tText.text = Toughness.toString();
			dText.text = Dexterity.toString();
			
			baseText.text = ( MAXBASE - baseTotal ).toString();
		}
		
		
		public function checkBase():void
		{
			if ( baseTotal == MAXBASE )
			{
				vRightButton.visible = false;
				sRightButton.visible = false;
				tRightButton.visible = false;
				dRightButton.visible = false;
				
				vLeftButton.visible = Vitality > 0;
				sLeftButton.visible = Strength > 0;
				tLeftButton.visible = Toughness > 0;
				dLeftButton.visible = Dexterity > 0;
			}
			else
			{
				vRightButton.visible = true;
				sRightButton.visible = true;
				tRightButton.visible = true;
				dRightButton.visible = true;
				
				vLeftButton.visible = Vitality > 0;
				sLeftButton.visible = Strength > 0;
				tLeftButton.visible = Toughness > 0;
				dLeftButton.visible = Dexterity > 0;
			}
			
			setBase();
		}
		
		
		public function setAttr():void
		{
			earthMask.width = earth * 8.1;
			waterMask.width = water * 8.1;
			fireMask.width = fire * 8.1;
			windMask.width = wind * 8.1;
			
			attrText.text = (MAXTOTAL - attrTotal).toString();
		}
		
		
		public function checkAttr():void
		{
			if ( attrTotal == MAXTOTAL )
			{
				earthRightButton.visible = false;
				waterRightButton.visible = false;
				fireRightButton.visible = false;
				windRightButton.visible = false;
				
				earthLeftButton.visible = earth > 0;
				waterLeftButton.visible = water > 0;
				fireLeftButton.visible = fire > 0;
				windLeftButton.visible = wind > 0;
			}
			else
			{
				earthRightButton.visible = true;
				waterRightButton.visible = true;
				fireRightButton.visible = true;
				windRightButton.visible = true;
				
				if ( earth )
				{
					earthLeftButton.visible = true;
					earthRightButton.visible = true;
					
					fireRightButton.visible = false;
					fireLeftButton.visible = false;
				}
				else
				{
					earthLeftButton.visible = false;
				}
				
				if ( water )
				{
					waterLeftButton.visible = true;
					waterRightButton.visible = true;
					
					windRightButton.visible = false;
					windLeftButton.visible = false;
				}
				else
				{
					waterLeftButton.visible = false;
				}
				
				if ( fire )
				{
					fireLeftButton.visible = true;
					fireRightButton.visible = true;
					
					earthRightButton.visible = false;
					earthLeftButton.visible = false;
				}
				else
				{
					fireLeftButton.visible = false;
				}
				
				if ( wind )
				{
					windLeftButton.visible = true;
					windRightButton.visible = true;
					
					waterRightButton.visible = false;
					waterLeftButton.visible = false;
				}
				else
				{
					windLeftButton.visible = false;
				}
			}
			
			setAttr();
		}
		
		
		private function onVRight( event:MouseEvent ):void
		{
			Vitality++;
			baseTotal++;
			
			checkBase();
		}
		
		private function onVLeft( event:MouseEvent ):void
		{
			Vitality--;
			baseTotal--;
			
			checkBase();
		}
		
		private function onSRight( event:MouseEvent ):void
		{
			Strength++;
			baseTotal++;
			
			checkBase();
		}
		
		private function onSLeft( event:MouseEvent ):void
		{
			Strength--;
			baseTotal--;
			
			checkBase();
		}
		
		private function onTRight( event:MouseEvent ):void
		{
			Toughness++;
			baseTotal++;
			
			checkBase();
		}
		
		private function onTLeft( event:MouseEvent ):void
		{
			Toughness--;
			baseTotal--;
			
			checkBase();
		}
		
		private function onDRight( event:MouseEvent ):void
		{
			Dexterity++;
			baseTotal++;
			
			checkBase();
		}
		
		private function onDLeft( event:MouseEvent ):void
		{
			Dexterity--;
			baseTotal--;
			
			checkBase();
		}
		
		private function onEarthRight( event:MouseEvent ):void
		{
			earth++;
			attrTotal++;
			
			checkAttr();
		}
		
		private function onEarthLeft( event:MouseEvent ):void
		{
			earth--;
			attrTotal--;
			
			checkAttr();
		}
		
		private function onWaterRight( event:MouseEvent ):void
		{
			water++;
			attrTotal++;
			
			checkAttr();
		}
		
		private function onWaterLeft( event:MouseEvent ):void
		{
			water--;
			attrTotal--;
			
			checkAttr();
		}
		
		private function onFireRight( event:MouseEvent ):void
		{
			fire++;
			attrTotal++;
			
			checkAttr();
		}
		
		private function onFireLeft( event:MouseEvent ):void
		{
			fire--;
			attrTotal--;
			
			checkAttr();
		}
		
		private function onWindRight( event:MouseEvent ):void
		{
			wind++;
			attrTotal++;
			
			checkAttr();
		}
		
		private function onWindLeft( event:MouseEvent ):void
		{
			wind--;
			attrTotal--;
			
			checkAttr();
		}
		
	}
	
}

